package com.sjf.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjf.common.Result;
import com.sjf.reggie.entity.Employee;
import com.sjf.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 员工控制器
 * @author <huangtang>
 * @since 2022/4/25 11:51
 **/
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {


    @Autowired
    private EmployeeService employeeService;

    /**
     * 查询员工
     * @param page 分页
     * @param size 每页数量
     * @param name 姓名
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page( @RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "pageSize") Integer size,
                              String name){
        log.info ("分页查询, page:{}, size:{}, name:{}", page, size, name);
        Page<Employee> result = employeeService.selectList (page, size, name);
        return Result.success (result);
    }

    /**
     * 修改员工信息
     * @param employee 员工
     * @return 修改结果
     */
    @RequestMapping(value = "",method = RequestMethod.PUT)
    public Result editEmployee(HttpServletRequest request,@RequestBody  Employee employee){
        log.info ("修改员工信息:{}", employee);
        boolean result = employeeService.edit (request,employee);
        long id = Thread.currentThread ().getId ();
        log.info ("当前线程id为{}", id);
        return result? Result.success ("修改成功") : Result.error ("修改失败");
    }

    /**
     * 获取员工信息
     * @param id 员工id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryEmployeeById(@PathVariable("id") Long id){
        log.info ("根据id查看员工:{}", id);
        Employee employee = employeeService.getById (id);
        return Result.success (employee);
    }

    /**
     * 新增员工
     * @param employee 员工
     * @return
     */
    @PostMapping()
    public Result save(HttpServletRequest request,@RequestBody  Employee employee){
        log.info ("新增员工:{}", employee);
        boolean result = employeeService.add (request,employee);
        return result? Result.success ("新增成功") : Result.error ("新增失败");
    }

    /**
     * 员工登录
     * @param request 请求
     * @param employee 员工
     * @return
     */
    @PostMapping("/login")
    public Result<Employee> login( HttpServletRequest request, @RequestBody Employee employee ){

        log.info("username:{}, password:{}", employee.getUsername (), employee.getPassword ());
        Result<Employee> result = employeeService.login (request , employee);
        return result.getCode () == 500 ? Result.error (result.getMsg ()) : Result.success (result.getData ());
    }

    /**
     * 注销
     * @param request 请求
     * @return 注销结果
     */
    @PostMapping("/logout")
    public Result logout( HttpServletRequest request ){
        employeeService.logout (request);
        return Result.success ("注销成功");
    }
}
